#include <cstring>
#include <dirent.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstring>
#include <stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
using namespace std;

char **readSourceFileName(const char *path); //读取源文件名称
int readFilenum(const char *path); //读取文件夹内.cpp/.h源文件数量
int traverseAST(char **path,int num);

int main(int argc, char *argv[]) {

  int i,j;
  char *filepath = argv[1];
  const char *filepath2 = "/home/song/Desktop/a/traverse/test/mec/";
   char **filename = readSourceFileName(filepath);
  int filenum = readFilenum(filepath);
  for (j = 0; j < filenum; j++) {
    cout << "filename: " << filename[j] << endl;
  }
  i=traverseAST(filename,filenum);
  
  return 0;
  }
  
  char **readSourceFileName(const char *path) //读取源文件名称
{
  char *newpath = new char[50];
  strcpy(newpath, path);
  char **namelist;
  DIR *dp;
  struct dirent *dirp;
  int n = 0, i, j, flagname = 0;
  namelist = (char **)malloc(20 * sizeof(char *));
  for (i = 0; i < 20; i++)
    namelist[i] = (char *)malloc(256 * sizeof(char));

  if ((dp = opendir(path)) == NULL) {
    cout << "can't open files" << endl;
  }

  while (((dirp = readdir(dp)) != NULL) && (n <= 50)) {
    int i, j;

    if (memcmp(".", dirp->d_name, 1) == 0) //忽略.和..文件夹
    {
    } else {
      for (i = 0, j = strlen(dirp->d_name) - 1; i < j; i++, j--) { //文件名逆序
        char c = dirp->d_name[i];
        dirp->d_name[i] = dirp->d_name[j];
        dirp->d_name[j] = c;
      }
      if ((memcmp(dirp->d_name, "c", 1) == 0) ||
          (memcmp(dirp->d_name, "h", 1) == 0)) //文件名结尾是c/h，保存下来
      {
        for (i = 0, j = strlen(dirp->d_name) - 1; i < j;
             i++, j--) { //文件名逆序
          char c = dirp->d_name[i];
          dirp->d_name[i] = dirp->d_name[j];
          dirp->d_name[j] = c;
        }
        memcpy(namelist[flagname], dirp->d_name,
               20); //将.cpp文件名保存到namelist[]
        strcat(newpath, namelist[flagname]);
        strcpy(namelist[flagname], newpath);
        strcpy(newpath, path);
        flagname++;
      }
    }
    n++;
  }
  closedir(dp);
  return namelist;
}

int readFilenum(const char *path) //读取文件夹内.c源文件数量
{
  DIR *dp;
  struct dirent *dirp;
  int n = 0, i, j;
  if ((dp = opendir(path)) == NULL)
    return -1; //-1表示未正常打开文件夹

  while (((dirp = readdir(dp)) != NULL) && (n <= 50)) {
    if (memcmp(".", dirp->d_name, 1) == 0) {
    } else {
      for (i = 0, j = strlen(dirp->d_name) - 1; i < j; i++, j--) { //文件名逆序
        char c = dirp->d_name[i];
        dirp->d_name[i] = dirp->d_name[j];
        dirp->d_name[j] = c;
      }
      if ((memcmp(dirp->d_name, "c", 1) == 0) ||
          (memcmp(dirp->d_name, "h", 1) == 0)) //结尾是c/h的要计数，xxx.cc/.hh
      {
        n++;
      }
    }
  }

  closedir(dp);
  return n;
}

int traverseAST(char **path,int num) 
{
    int i,j;
    
    for(j=0;j<num;j++)
    {
        char* code=new char[100];
        strcpy(code,"/home/song/Desktop/a/traverse/build/traverse ");
        strcat(code,path[j]);
        cout<<code<<endl;
     //   system(code);
    }
return 0;
}